package cn.t.facade.android;

import cn.t.constants.Constants;
import cn.t.constants.ParamConstants;
import cn.t.constants.ResponseError;
import cn.t.core.exception.BizException;
import cn.t.dto.user.RoleSkuDTO;
import cn.t.dto.user.SkuQtyDTO;
import cn.t.facade.user.RoleSkuFacade;
import cn.t.model.order.OrderDO;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RBucket;
import org.redisson.api.RedissonClient;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;


/**
 * @author l
 */
@Slf4j
@Service
@AllArgsConstructor
public class AndroidSessionFacade {
    @Resource
    private RedissonClient redissonClient;
    @Resource
    private DeviceCommunicationFacade deviceCommunicationFacade;
    @Resource
    private RoleSkuFacade roleSkuFacade;
    /**
     * 获取用户可领用数量
     */
    public List<SkuQtyDTO> getDayOverPickNumSession(OrderDO orderBean, Boolean session) {
        //用户领用数量key
        if (session){
                try {
                    String deviceTokenKey = String.format(Constants.REDIS_KEY_DEVICE_ORDER_DATA, orderBean.getUserId(), orderBean.getFirmId());
                    RBucket<Object> bucket = redissonClient.getBucket(deviceTokenKey);
                    if (bucket.isExists() ) {
                        return (List<SkuQtyDTO>) bucket.get();
                    }
                }catch (Exception e){
                    log.error("获取用户可领用数量失败",e);;
                }
        }
            List<RoleSkuDTO>  skuRoleList= roleSkuFacade.findSkuByParticipantId(orderBean.getFirmId(), orderBean.getParticipantId(), orderBean.getDeviceId());
            return deviceCommunicationFacade.getDayOverPickNumList(orderBean.getUserId(),orderBean.getFirmId(),skuRoleList);

    }

    /**
     * 设备领用中不允许开其他柜子
     */
    public void devicePickUp(Long userId, Long firmId) {
        String orderSnKey = String.format(Constants.REDIS_KEY_DEVICE_ORDER_ORDER_DATA,userId,firmId);
        RBucket<String> bucket = redissonClient.getBucket(orderSnKey);
        if (bucket.isExists()){
            throw new BizException(ResponseError.OPEN_ORDER_ERR, String.format(ResponseError.OPEN_ORDER_ERR.getName(), bucket.get() ));
        }
    }
    public void setDayOverPickNumSession(Long userId, Long firmId,Long participantId,Long deviceId) {
        List<RoleSkuDTO>  skuRoleList= roleSkuFacade.findSkuByParticipantId(firmId,participantId, deviceId);
        List<SkuQtyDTO> dayOverPickNumList = deviceCommunicationFacade.getDayOverPickNumList(userId,firmId, skuRoleList);
        //用户领用数量key
        String deviceTokenKey = String.format(Constants.REDIS_KEY_DEVICE_ORDER_DATA, userId, firmId);
        String orderSnKey = String.format(Constants.REDIS_KEY_DEVICE_ORDER_ORDER_DATA, userId, firmId);
        redissonClient.getBucket(deviceTokenKey).set(dayOverPickNumList, 1, TimeUnit.HOURS);
        redissonClient.getBucket(orderSnKey).set(deviceId, ParamConstants.REDISORDERKEYTIMEOUT, TimeUnit.MINUTES);
    }



    public void deleteDayOverPickNumSession(Long userId, Long firmId) {
         //用户领用数量key
        String deviceTokenKey = String.format(Constants.REDIS_KEY_DEVICE_ORDER_DATA, userId, firmId);
        String orderSnKey = String.format(Constants.REDIS_KEY_DEVICE_ORDER_ORDER_DATA, userId,firmId);
        redissonClient.getBucket(deviceTokenKey).delete();
        redissonClient.getBucket(orderSnKey).delete();
    }
    }
